const WebSocket = require('ws');

class SimpleWSTester {
    constructor() {
        this.ws = null;
    }

    async connect() {
        return new Promise((resolve, reject) => {
            console.log('🔌 正在连接WebSocket...');
            this.ws = new WebSocket('ws://localhost:3000/user-service/websocket');
            
            this.ws.on('open', () => {
                console.log('✅ WebSocket连接成功');
                resolve();
            });
            
            this.ws.on('error', (error) => {
                console.error('❌ WebSocket连接失败:', error.message);
                reject(error);
            });

            this.ws.on('message', (message) => {
                try {
                    const data = JSON.parse(message.toString());
                    console.log('📨 收到消息:', JSON.stringify(data, null, 2));
                } catch (error) {
                    console.log('📨 收到原始消息:', message.toString());
                }
            });
        });
    }

    async testBasicConnection() {
        console.log('🧪 测试基本连接...');
        
        // 发送一个简单的测试消息
        this.ws.send(JSON.stringify({
            c: 1005,
            d: { ap: 0, hp: 0, mp: 0, sp: 0 }
        }));
        
        console.log('📤 已发送测试消息');
    }

    async disconnect() {
        if (this.ws) {
            this.ws.close();
            console.log('✅ WebSocket连接已关闭');
        }
    }
}

async function main() {
    const tester = new SimpleWSTester();
    
    try {
        await tester.connect();
        await tester.testBasicConnection();
        
        // 等待5秒看是否有响应
        await new Promise(resolve => setTimeout(resolve, 5000));
        
    } catch (error) {
        console.error('❌ 测试失败:', error.message);
    } finally {
        await tester.disconnect();
    }
}

main();


